/*
  Name: DeadProxyPacketQueue.cpp
  Copyright:
  Author: Carl Chatfield
  Date: 19/01/08 22:21
  Description: Packet Queue which can be used by Send Queue to store packets
*/

#include "DeadProxyPacketQueue.h"
#include <wx/arrimpl.cpp> //this is needed for wxArrays, i dont know why lol
WX_DEFINE_OBJARRAY(ArrayOfPacketBuffers);

PacketQueue::PacketQueue()
{
    Queue = new ArrayOfPacketBuffers;
    mutex = new wxMutex();
    mutex->Unlock();
}

PacketQueue::~PacketQueue()
{
    mutex->Lock();
    delete Queue;
    mutex->Unlock();
    delete mutex;
}

void PacketQueue::AddPacket(PacketBuffer * packet)
{
    mutex->Lock();
    Queue->Add(packet);
    mutex->Unlock();
}

PacketBuffer * PacketQueue::GetPacket()
{
    PacketBuffer * packet;
    mutex->Lock();
    if(Queue->GetCount() == 0)
    {
        packet = 0;
    } else {
        packet = Queue->Detach(0);
    }
    mutex->Unlock();
    return packet;
}

int PacketQueue::GetCount()
{
    int len;
    mutex->Lock();
    len = Queue->GetCount();
    mutex->Unlock();
    return len;
}

void PacketQueue::Clear()
{
    mutex->Lock();
    Queue->Clear();
    mutex->Unlock();
}
